package cn.qidd.keep.manage.controller.system;

import cn.qidd.keep.domain.core.Result;
import cn.qidd.keep.domain.document.OperationLog;
import cn.qidd.keep.domain.document.OperationLogDetails;
import cn.qidd.keep.domain.model.OperationLogModel;
import cn.qidd.keep.domain.web.BaseController;
import cn.qidd.keep.manage.model.system.OperationLogParams;
import cn.qidd.keep.manage.repository.OperationLogDetailsRepository;
import cn.qidd.keep.manage.repository.OperationLogRepository;
import com.querydsl.core.BooleanBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Optional;

/**
 * Created by sunyanping on 2019/3/5
 */
@RestController
@RequestMapping("/api/operation-log")
@Api(description = "操作日志", value = "OperationLogController")
public class OperationLogController extends BaseController {

    @Autowired
    private OperationLogRepository operationLogRepository;
    @Autowired
    private OperationLogDetailsRepository operationLogDetailsRepository;

    @GetMapping("/findOperationLogsByPage")
    @ApiOperation(value = "分页查询系统操作日志", notes = "分页查询系统操作日志")
    public Result<Page<OperationLog>> findOperationLogsByPage(@ApiIgnore Pageable pageable,
                                                              OperationLogParams params) {
        BooleanBuilder builder = params.generateSearchCondition();
        Sort sort = new Sort(Sort.Direction.DESC, "operatorTime");
        Pageable pageable1 = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort);
        Page<OperationLog> operationLogs = operationLogRepository.findAll(builder, pageable1);
        return new Result<>(Result.Status.SUCCESS, operationLogs);
    }

    @GetMapping("/findOperationLogsDetails/{detailsId}")
    @ApiOperation(value = "查看日志详细错误", notes = "查看日志详细错误")
    public Result<OperationLogDetails> findOperationLogsDetails(@PathVariable String detailsId) {
        Optional<OperationLogDetails> logDetails = operationLogDetailsRepository.findById(detailsId);
        return new Result<>(Result.Status.SUCCESS, logDetails.orElse(null));
    }

    @ApiIgnore
    @PostMapping("/addOperationLog")
    public void addOperationLog(@RequestBody OperationLogModel model) {
        OperationLog operationLog = new OperationLog();

        BeanUtils.copyProperties(model, operationLog);
        if (model.getText() != null) {
            OperationLogDetails details = new OperationLogDetails();
            details.setText(model.getText());
            OperationLogDetails operationLogDetails = operationLogDetailsRepository.save(details);
            operationLog.setDetailsId(operationLogDetails.getId());
        }
        operationLogRepository.save(operationLog);
    }
}
